package com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.UrlFilter;
import com.service.IUrlFilterService;
import com.util.Message;

@Controller
public class UrlFilterController {
	@Resource
	private IUrlFilterService urlFilterService;

	// 分页
	@RequestMapping(value = "/allUrlFiltersByPage", method = RequestMethod.POST)
	@ResponseBody
	public Message geturlFilters(@RequestBody Message message) {
		List<UrlFilter> list = urlFilterService.getUrlFilters(Integer.valueOf(message.getMessage()),
				message.getGroups());
		// 重新获取总数，保证数据事实刷新
		int count = urlFilterService.getCounts();
		Message mess = new Message();
		mess.setUrlFilter(list);
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/allUrlFilters", method = RequestMethod.GET)
	@ResponseBody
	public List<UrlFilter> getallurlFilters() {
		List<UrlFilter> list = urlFilterService.getUrlFilters();
		return list;
	}

	// 获取总页数
	@RequestMapping(value = "/getUrlFilterCounts", method = RequestMethod.POST)
	@ResponseBody
	public Message getcounts(@RequestBody Message message) {
		// 获取总数
		int count = urlFilterService.getCounts();
		Message mess = new Message();
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/deleteUrlFilter/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteurlFilter(@PathVariable int id) {
		urlFilterService.deleteUrlFilters(id);
		urlFilterService.deleteUrlFilter(id);
		return new Message("success");
	}

	@RequestMapping(value = "/addUrlFilter", method = RequestMethod.POST)
	@ResponseBody
	public UrlFilter addurlFilter(@RequestBody UrlFilter u) {
		UrlFilter per = urlFilterService.createUrlFilter(u);
		return per;
	}

	@RequestMapping(value = "/updateUrlFilter", method = RequestMethod.PUT)
	@ResponseBody
	public UrlFilter updateurlFilter(@RequestBody UrlFilter u) {
		UrlFilter per = urlFilterService.updateUrlFilter(u);
		return per;
	}

	@RequestMapping(value = "/getUrlFilter/{id}", method = RequestMethod.GET)
	@ResponseBody
	public UrlFilter getUrlFilter(@PathVariable int id) {
		UrlFilter p = urlFilterService.getUrlFilterByid(id);
		return p;
	}
}
